App({
  onLaunch() {
    this.checkLogin((res) => {
      if (!res.isLogin) {
        this.login()
      }
    })
  },
  login() {
    wx.login({
      success: (res) => {
        wx.request({
          url: 'http://127.0.0.1:2810/login',
          method: "POST",
          data: {
            code: res.code
          },
          success: res => {
            const token = res.data.token
            this.globalData.token = token
            wx.setStorageSync('token', token)
          }
        })
      },
    })
  },
  checkLogin(fun) {
    let token = this.globalData.token
    if (!token) {
      token = wx.getStorageSync('token')
      if (token) {
        this.globalData.token = token
      } else {
        fun({
          isLogin: false
        })
        return
      }
    }
    wx.request({
      url: 'http://127.0.0.1:2810/checklogin',
      method: "get",
      data: {
        token: token
      },
      success: res => {
        console.log(res);
        fun({
          isLogin: res.data.isLogin
        })
      }
    })
  },

  globalData: {
    token: null
  }
})
